//Given a list of WordNet nouns x1, x2, ..., xn, which noun is the least related to the others? 
//To identify an outcast, compute the sum of the distances between each noun and every other one
//计算每个名词与其他每个名词之间的距离总和


import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;

public class Outcast {
    private WordNet wordNet;

    public Outcast(WordNet wordnet) // constructor takes a WordNet object
    {
        this.wordNet = wordnet;
    }

    // 给一个wordNet的名词数组，返回数组里最不相关的那一个，也就是outcast 参数最少包含两个名词
    public String outcast(String[] nouns) // given an array of WordNet nouns, return an outcast
    {
        // i循环是对x，k循环是对每个x得出distance
        int distanceTmp = 0;
        int distanceMax = 0;
        int idMax = 0;
        for (int i = 0; i < nouns.length; i++) {
            distanceTmp = 0;
            for (int k = 0; k < nouns.length; k++) { 
                distanceTmp += wordNet.distance(nouns[i], nouns[k]);
            }
            if (distanceTmp > distanceMax) {
                distanceMax = distanceTmp;
                idMax = i;
            }
        }
        return nouns[idMax];
    }

    public static void main(String[] args) // see test client below
    {
        WordNet wordnet = new WordNet("synsets.txt", "hypernyms.txt");
        Outcast outcast = new Outcast(wordnet);
        In in = new In("input.txt");
        String[] nouns = in.readAllStrings();
        StdOut.println("input.txt" + ": " + outcast.outcast(nouns));
    }
}
